package com.jb.repair.acticity;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.jb.repair.R;
import com.jb.repair.common.AppConfig;
import com.jb.repair.dao.Db;
import com.jb.repair.model.FssbModel;
import com.jb.repair.model.SbbdzdyModel;
import com.jb.repair.util.DownAttachmentDialogUtil;
import com.jb.repair.util.DownFileAsyncTask;
import com.jb.repair.util.IntentUtils;
import com.jb.repair.util.StringUtils;
import com.jb.repair.view.InfoTextView;
import com.jb.repair.view.TitleView;

import java.io.File;
import java.util.List;

/**
 * Created by gaobin on 2015/10/27.
 * 附属设备详情页
 */
public class FssbIntroActivity extends BaseActivity {
    private TitleView titleView;
    private InfoTextView tv_fssb, tv_sbxh, tv_sl, tv_tyrq, tv_img, tv_sccj, tv_zycs, tv_note, tv_code;
    private Db db;
    private LinearLayout ll_fssb;
    private FssbModel model;
    private int i = -1;
    private DownAttachmentDialogUtil dialogUtil;

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fsxbxq);
        initView();
        db = Db.getInstance(this);
        String form_CODE = db.getName2("CODE", "TB_SYS_CODE", "CODE_NAME", "附属设备");
        //  Log.e("1231",form_CODE);

        model = (FssbModel) getIntent().getSerializableExtra("fssb");
        String id_zhuanye = getIntent().getStringExtra("id_zhuanye");
        tv_fssb.setText(model.attacheq_name);

        String visible1 = db.getVisible(form_CODE, "producer_editable");
        // Log.e("1231",visible1);
        if (!visible1.equals("T")) {
            tv_sccj.setVisibility(View.GONE);
        }
        tv_sccj.setText(model.producer_editable);

        String visible2 = db.getVisible(form_CODE, "operation_date_editable");
        //  Log.e("1231",visible2);
        if (!visible2.equals("T")) {
            tv_tyrq.setVisibility(View.GONE);
        }
        tv_tyrq.setText(model.operation_date_editable);
        tv_sbxh.setText(model.model);
        String visible3 = db.getVisible(form_CODE, "num_editable");
        // Log.e("1231",visible3);
        if (!visible3.equals("T")) {
            tv_sl.setVisibility(View.GONE);
        }
        tv_sl.setText(model.num_editable);
        //      Log.e("tv_attachment", model.attachment);
        String name2 = db.getName("WJMC", "TB_FILE_LB", "GUID", model.attachment);
        if (!StringUtils.isNullOrEmpty(name2)) {
            tv_img.setText(name2);
            i = 1;
        } else {
            String name4 = db.getName("WJMC", "TB_FILE_LB", "OBJ_ID", model.attachment);
            tv_img.setText(name4);
            i = 2;
        }

        String visible4 = db.getVisible(form_CODE, "tech_parameter_editable");
        //  Log.e("1231",visible4);
        if (!visible4.equals("T")) {
            tv_zycs.setVisibility(View.GONE);
        }
        tv_zycs.setText(model.tech_parameter_editable);

        String visible5 = db.getVisible(form_CODE, "code_editable");
        //     Log.e("1231",visible5);
        if (!visible5.equals("T")) {
            tv_code.setVisibility(View.GONE);
        }
        tv_code.setText(model.code_editable);
        tv_note.setText(model.note);

        //----------------------------------------------处理自定义字段逻辑-----------------------------------------------------------
        //查找自定义字段显示
        //拿到表单编码
        List<SbbdzdyModel> sbbdzdyModelList = db.getSbbdzdyModel(form_CODE, id_zhuanye);
        for (SbbdzdyModel model1 : sbbdzdyModelList) {
            if (model1.is_show_eqbook.equals("T")) {
                InfoTextView infoTextView = new InfoTextView(this);
                ll_fssb.addView(infoTextView);
                //需要设置key的值
                infoTextView.setKey(model1.cus_name);
                //设置value的值

                String format = model1.field_format;
                String value = db.getZdyzdValue(model1.guid, model.guid);
                if (value == null) {
                    value = "";
                } else if (format != null && "时间".equals(format) && value.length() > 10) {
                    value = value.substring(0, 10);
                }
                infoTextView.setText(value);

                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.topMargin = (int) getResources().getDimension(R.dimen.ledger_info_textsize);
                infoTextView.setLayoutParams(params);
            }

        }
        tv_img.setOnClickListener(new View.OnClickListener(
        ) {
            public void onClick(View view) {

                Log.i("点击测试", "收到点击事件");
                //附件的guid
                final String guid;
                //附件的文件类型
                final String fjlx;
                if (i == 1) {
                    guid = db.getName("GUID", "TB_FILE_LB", "GUID", model.attachment);
                    fjlx = db.getName("LX", "TB_FILE_LB", "GUID", model.attachment);
                } else {
                    guid = db.getName("GUID", "TB_FILE_LB", "OBJ_ID", model.attachment);
                    fjlx = db.getName("LX", "TB_FILE_LB", "OBJ_ID", model.attachment);
                }
                //判断是否有附件
                boolean nullOrEmpty = StringUtils.isNullOrEmpty(tv_img.getText());
                Log.e("LLL", String.valueOf(nullOrEmpty));
                if (nullOrEmpty) {
                    return;
                } else {
                    //有附件
                    String filePath = AppConfig.ATTACHMENT_PATh + guid + "." + fjlx;
                    Log.e("1111", filePath);
                    File file = new File(filePath);
                    if (!file.exists()) {
                        Toast.makeText(FssbIntroActivity.this, "文件不存在", Toast.LENGTH_SHORT).show();
                        dialogUtil = new DownAttachmentDialogUtil(FssbIntroActivity.this, R.style.myDialog,
                                new DownAttachmentDialogUtil.AttachDialogListener() {
                                    @Override
                                    public void onClick(View v) {
                                        switch (v.getId()) {
                                            case R.id.attach_dialog_canlebtn:
                                                dialogUtil.dismiss();
                                                break;
                                            case R.id.attach_dialog_surebtn:
                                                new DownFileAsyncTask(FssbIntroActivity.this, guid, model.attachment, fjlx).execute();


                                                dialogUtil.dismiss();

                                                break;
                                        }
                                    }

                                });
                        Window win = dialogUtil.getWindow();
                        win.setGravity(Gravity.CENTER);
                        dialogUtil.show();
                    } else {
                        IntentUtils.JumpToIntent(FssbIntroActivity.this, fjlx, filePath);

                    }

                }
            }
        });
    }


//-------------------------------------------------------------------------------------------------------


    private void initView() {
        titleView = (TitleView) findViewById(R.id.titleView);
        tv_fssb = (InfoTextView) findViewById(R.id.tv_info);
        tv_sbxh = (InfoTextView) findViewById(R.id.tv_version);
        tv_sl = (InfoTextView) findViewById(R.id.tv_num);
        tv_tyrq = (InfoTextView) findViewById(R.id.tv_run_time);
        tv_img = (InfoTextView) findViewById(R.id.tv_img);
        tv_sccj = (InfoTextView) findViewById(R.id.tv_factory);
        tv_zycs = (InfoTextView) findViewById(R.id.tv_main_param);
        tv_note = (InfoTextView) findViewById(R.id.tv_note);
        tv_code = (InfoTextView) findViewById(R.id.tv_code);
        ll_fssb = (LinearLayout) findViewById(R.id.ll_fssb);

        titleView.setOnClickListener(new TitleView.OnTitleViewClickListener() {
            @Override
            public void onLeftImageClick(View view) {
                super.onLeftImageClick(view);
                finish();
            }
        });
    }
}
